﻿using System;
using Csla;
using Csla.Serialization;
using Csla.Data;

namespace Accounting.Library.GhiSo
{
    [Serializable()]
    public class BoGhiService : BusinessBase<BoGhiService>
    {
        #region Business methods
        private static PropertyInfo<string> ErrorMessageProperty = RegisterProperty<string>(p => p.ErrorMessage, "ErrorMessage");
        public string ErrorMessage
        {
            get { return GetProperty(ErrorMessageProperty); }
            set { SetProperty(ErrorMessageProperty, value); }
        }
        #endregion

        #region  Factory Methods
        /// <summary>
        /// Dung cho nghiep vu bo ghi
        /// </summary>
        /// <param name="loaiChungTuId"></param>
        /// <param name="chungTuGocId"></param>
        /// <param name="callback"></param>
        public static void BoGhi(int loaiChungTuId, int chungTuGocId, EventHandler<DataPortalResult<BoGhiService>> callback)
        {
            var dp = new DataPortal<BoGhiService>();
            dp.FetchCompleted += callback;
            dp.BeginFetch(new BoGhiServiceCriteria(loaiChungTuId, chungTuGocId), callback);
        }

        /// <summary>
        /// Dung cho nghiep vu bo ghi
        /// </summary>
        /// <param name="loaiChungTuId"></param>
        /// <param name="chungTuGocId"></param>
        /// <param name="callback"></param>
        public static void BoGhi(int loaiChungTuId, int chungTuGocId, int nhaCungCapId, int phuongThucThanhToanId, double soTien, EventHandler<DataPortalResult<BoGhiService>> callback)
        {
            var dp = new DataPortal<BoGhiService>();
            dp.FetchCompleted += callback;
            dp.BeginFetch(new BoGhiServiceCriteria(loaiChungTuId, chungTuGocId, nhaCungCapId, phuongThucThanhToanId, soTien), callback);
        }

#if !SILVERLIGHT
        /// <summary>
        /// Xoa du lieu trong TH_SoChiTiet
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldType"></param>
        /// <param name="fieldValue"></param>
        /// <returns>Use IsExist property to determine the exist of record in database </returns>
        public static BoGhiService BoGhi(int loaiChungTuId, int chungTuGocId)
        {
            return DataPortal.Fetch<BoGhiService>(new BoGhiServiceCriteria(loaiChungTuId, chungTuGocId));
        }

        private BoGhiService()
        { /* require use of factory methods */ }

#else
        public BoGhiService()
        { /* require by MobileFormatter */ }
#endif

        #endregion

        #region Root object data access
#if !SILVERLIGHT
        private void DataPortal_Fetch(BoGhiServiceCriteria criteria)
        {
            try
            {
                using (var ctx =
                    ContextManager<Accounting.DataAccess.AccountingDataContext>.
                    GetManager(Accounting.DataAccess.Database.Accounting))
                {
                    //Xoa so chi tiet
                    ctx.DataContext.deleteTH_SoChiTiet(criteria.ChungTuGocId, criteria.LoaiChungTuId);

                    //Xoa cong no khach hang va nha cung cap
                    if (BusinessConfiguration.Configuration.LaChungTuGhiTangCongNo(criteria.LoaiChungTuId))
                    {
                        if (PhuongThucThanhToanService.ThanhToanBangCongNo(ctx, criteria.PhuongThucThanhToanId))
                        {
                            NhaCungCapService.GhiGiamCongNo(ctx, criteria.NhaCungCapId, criteria.SoTien);
                        }
                    }
                    else if (BusinessConfiguration.Configuration.LaChungTuGhiGiamCongNo(criteria.LoaiChungTuId)) 
                    {
                        NhaCungCapService.GhiTangCongNo(ctx, criteria.NhaCungCapId, criteria.SoTien);
                    }
                    else if (BusinessConfiguration.Configuration.LaChungTuGhiTangKyGui(criteria.LoaiChungTuId)) 
                    {
                        NhaCungCapService.GhiGiamKyGui(ctx, criteria.NhaCungCapId, criteria.SoTien);
                    }
                    else if (BusinessConfiguration.Configuration.LaChungTuGhiGiamKyGui(criteria.LoaiChungTuId)) 
                    {
                        NhaCungCapService.GhiTangKyGui(ctx, criteria.NhaCungCapId, criteria.SoTien);
                    }
                }
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.StackTrace);
            }
        }
#endif
        #endregion
    }
}
